package com.sd.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.alex.books.BaseBook;
import com.alex.books.Chapter;
import com.alex.books.ReaderingInfo;

public class ReadUtil {

	public static float getPadding(BaseBook book,int space){
		float padding = 0;
		if(book.mChapterNumber != null){
			float u = Float.valueOf(book.mReaderingInfo.mMarkPos) / Float.valueOf(book.mWordNumber);
			padding = (float)space*(u);
		}
		
		return padding;
	}
	
	public static String getPercent(BaseBook book){
		String percent = "000";
		if(book.mBookPath.endsWith(".txt") || book.mBookPath.endsWith(".sbn") || book.mBookPath.endsWith(".qd")){
			if(book.mChapterNumber != null){
				float u = Float.valueOf(book.mReaderingInfo.mMarkPos) / Float.valueOf(book.mWordNumber);
				DecimalFormat myformat = null; 
				myformat= (DecimalFormat)NumberFormat.getPercentInstance(); 
				myformat.applyPattern("0.00%"); //0表示加的小数点,00表示两位小数点，你用00试一下你就知道效果 
				//myformat.setMaximumFractionDigits(Integer.MAX_VALUE);//这个1的意识是保存结果到小数点后几位（精确度） 
				percent = myformat.format(u);
			}
		}else{
			float u = (float)book.mReaderingInfo.mIndex / Float.valueOf(book.mChapters.size());
			float u1 = u + Float.valueOf(book.mReaderingInfo.mMarkPos) / Float.valueOf(book.mReaderingInfo.mCurChapter.mLength)/Float.valueOf(book.mChapters.size());
			DecimalFormat myformat = null; 
			myformat= (DecimalFormat)NumberFormat.getPercentInstance(); 
			myformat.applyPattern("0.00%"); //0表示加的小数点,00表示两位小数点，你用00试一下你就知道效果 
			//myformat.setMaximumFractionDigits(Integer.MAX_VALUE);//这个1的意识是保存结果到小数点后几位（精确度） 
			percent = myformat.format(u1);
		}
		return percent;
	}

	
	public static ReaderingInfo getReaderInfo(BaseBook book,int total,int postion){
		if(postion==total){
			postion -= 1;
		}
		ReaderingInfo readeringInfo = new ReaderingInfo();
		if(book.mBookPath.endsWith(".txt") || book.mBookPath.endsWith(".sbn") || book.mBookPath.endsWith(".qd")){
			int n = (int)((Float.valueOf(book.mWordNumber)-1)*(float)postion/(float)total);
			Chapter chapter = null;
			int pos = 0;
			for(int i=0;i<book.mChapters.size();i++){
				 chapter = book.mChapters.get(i);
				 pos += chapter.mChapterName.length() + chapter.mLength;
				 if(n<pos){
					 break;
				 }
			}
			readeringInfo.mCurChapter = chapter;
			readeringInfo.mIndex = chapter.mChapterIndex;
			readeringInfo.mMarkPos = n;
		}else{
			int pos = Integer.valueOf(book.mWordNumber)*postion/total;
			for(int i=0;i<book.mChapters.size();i++){
				Chapter chapter = book.mChapters.get(i);
				if(pos<chapter.mLength){
					readeringInfo.mCurChapter = chapter;
					readeringInfo.mIndex = chapter.mChapterIndex;
					readeringInfo.mMarkPos = pos;
					break;
				}
				pos -= chapter.mLength;
			}
		}
		return readeringInfo;
	}
	
	public static ReaderingInfo getReaderInfo2(BaseBook book,int total,int postion,int pos0){
		int n = (book.mChapters.size()-1) * postion / total;
		ReaderingInfo readeringInfo = new ReaderingInfo();
		if(book.mBookPath.endsWith(".txt") && book.mBookPath.endsWith(".sbn") || book.mBookPath.endsWith(".qd")){
			Chapter chapter = book.mChapters.get(n);
			
			readeringInfo.mCurChapter = chapter;
			readeringInfo.mIndex = chapter.mChapterIndex;
			readeringInfo.mMarkPos = book.mReaderingInfo.mMarkPos + pos0 * Long.valueOf(book.mWordNumber)/10000;
			if(readeringInfo.mMarkPos <0){
				readeringInfo.mMarkPos = 0;
			}else if(readeringInfo.mMarkPos >= Long.valueOf(book.mWordNumber)){
				readeringInfo.mMarkPos = Long.valueOf(book.mWordNumber) - pos0 * Long.valueOf(book.mWordNumber)/10000;
			}
		}else{
			int pos = Integer.valueOf(book.mWordNumber)*postion/total;
			for(int i=0;i<book.mChapters.size();i++){
				Chapter chapter = book.mChapters.get(i);
				if(pos<chapter.mLength){
					readeringInfo.mCurChapter = chapter;
					readeringInfo.mIndex = chapter.mChapterIndex;
					readeringInfo.mMarkPos = pos;
					break;
				}
				pos -= chapter.mLength;
			}
		}
		return readeringInfo;
	}
	
	
	public static String getCurrentTime(){
	       Date date = new Date(System.currentTimeMillis());
	       SimpleDateFormat format1 = new SimpleDateFormat("HH:MM,EEEE, d.MMMM yyyy ");
	       return format1.format(date);
	}
}
